﻿
Namespace Config

    Public Class MultiPlugConfigConfigurationSectionHandler
        Implements System.Configuration.IConfigurationSectionHandler

        Public Const XSD_PATH As String = "Xml\XmlSchema\multiPlugPlugins.xsd"

        Public Const TAG_CONNECTION_NAME As String = "connectionName"
        Public Const TAG_CONNECTION_STRING As String = "connectionString"
        Public Const TAG_CONNECTION_PROVIDER As String = "connectionProvider"

        Public Const TAG_DATA_ASSEMBLY As String = "dataAssembly"
        Public Const TAG_SERVICE_TYPES As String = "serviceTypes"
        Public Const TAG_SERVICE_TYPE As String = "serviceType"
        Public Const ATT_SERVICE_NAME As String = "name"
        Public Const ATT_SERVICE_TYPE As String = "type"

        Public Function Create(ByVal parent As Object, ByVal configContext As Object, ByVal section As System.Xml.XmlNode) As Object Implements System.Configuration.IConfigurationSectionHandler.Create

            ' den validator für die configSection erstellen
            'Dim xmlValidator As New Xml.XmlValidator(XSD_PATH, section.OuterXml)

            '' die configSection validieren lasse
            'xmlValidator.Validate()

            '' wenn das validieren fehlgeschlagen ist, dann den fehler werfen
            'If Not xmlValidator.Valid Then _
            '    Throw xmlValidator.Exception

            ' konfigurationsobjekt erstellen
            Dim mpc As New MultiPlugConfig

            ' configsection in xmlelement casten, damit man es besser auslesen kann
            Dim configSection As System.Xml.XmlElement = _
                CType(section, System.Xml.XmlElement)


            mpc.ConnectionName = configSection.GetElementsByTagName(TAG_CONNECTION_NAME)(0).InnerText.Trim()
            mpc.ConnectionString = configSection.GetElementsByTagName(TAG_CONNECTION_STRING)(0).InnerText.Trim()
            mpc.ConnectionProvider = configSection.GetElementsByTagName(TAG_CONNECTION_PROVIDER)(0).InnerText.Trim()

            mpc.DataAssemblyPath = configSection.GetElementsByTagName(TAG_DATA_ASSEMBLY)(0).InnerText.Trim()


            For Each serviceTypes As System.Xml.XmlElement In configSection.GetElementsByTagName(TAG_SERVICE_TYPE)

                mpc.ServiceTypes.Add(New ServiceType(serviceTypes.GetAttribute(ATT_SERVICE_NAME).Trim(), _
                                                     serviceTypes.GetAttribute(ATT_SERVICE_TYPE).Trim()))

            Next


            Return mpc

        End Function

    End Class

End Namespace